package com.stu.manager.stuScore.service;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stu.manager.config.Result;
import com.stu.manager.scoreinfo.entity.SysScore;
import com.stu.manager.scoreinfo.mapper.SysScoreMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudentFindScoreService {
    @Resource
    private SysScoreMapper scoreMapper;


    //学生登录后获取各年份列表
    public Result gradeInfo(String stuNumber) {
        String substring = stuNumber.substring(0, 4);//2016
        int stuNum = Integer.parseInt(substring);
        Date date = DateUtil.date();
        int currentYear = DateUtil.year(date);//当前年

        List<Integer> list = new ArrayList<>();
        int i = currentYear - stuNum;//年份差额
        if (i > 0) {
            if (i < 4) {//还在上学4年内
                for (int i1 = 0; i1 < i; i1++) {
                    if (stuNum > currentYear) {//如果到了当前年那么终止循环
                        break;
                    }
                    list.add(stuNum);
                    stuNum = stuNum + 1;
                }
            } else {
                for (int i1 = 0; i1 < 4; i1++) {
                    list.add(stuNum);
                    stuNum = stuNum + 1;
                }
            }
            return Result.ok().data("grades",list);
        }else{
            return Result.error().message("当前学生年份异常！");
        }
    }


    //查单年的成绩
    public Result scoreInfo(String classGrade,String stuNumber) {
        QueryWrapper<SysScore> wrapper = new QueryWrapper<>();
        wrapper.eq("stu_number",stuNumber).eq("class_grade",classGrade);
        List<SysScore> sysScores = scoreMapper.selectList(wrapper);

        return Result.ok().data("item",sysScores);

    }
}
